<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head th:include="common/admin/adminHeader::html('属性管理')">
</head>

<body>
<!-- 导航栏 -->
<div th:replace="common/admin/adminNavigator::html"></div>


<script>

    $(function(){
        var cid = getUrlParms("cid");
        var data4Vue = {
            uri:"property",
            beans:[], //用于存放返回的json数据
            property:{     //用于存放属性的信息
                id:0,
                cid: cid,
                name:""
            },
            category:{ //用于存放分类信息，指明这些属性时哪一个分类下
                id: cid,
                name:''
            }

        };

        //ViewModel
        var vue = new Vue({
            el: '#container-fluid',
            data: data4Vue,
            mounted:function(){ //mounted　表示这个 Vue 对象加载成功了
                this.list(0);
            },
            methods: {
                list:function(pn){
                    ///listPage?cid=80&pn=1
                    var url =  this.uri + "/listPage?cid=" + cid + "&pn=" + pn;
                    axios.get(url).then(function(response) {
                        vue.beans = response.data.data;
                    });

                    var url2 = "category/" + cid;
                    axios.get(url2).then(function(response) {
                        vue.category = response.data.data;
                    });
                },
                jump:function (page) {
                    jump(page,vue);
                },
                jumpByNumber:function (pn) {
                    jumpByNumber(pn,vue);
                },
                add:function () {
                    //判断是否存在属性信息
                    if (!checkEmpty(this.property.name,"属性名称"))
                        return;
                    var url = this.uri + "/create";
                    axios.post(url, this.property).then(function(response){
                        //结束后查询最后一页
                        vue.list(999);
                        //清空文件上传相关数据
                        vue.property = { id: 0, cid: cid, name: ''};
                    });
                },
                deleteProperty:function (id) {
                    if (!checkDeleteLink()){
                        return;
                    }
                    var url = this.uri + "/" + id;
                    axios.delete(url).then(function (value) {
                        if(0!=value.data.length){
                            alert("成功删除"+ id + "属性");
                            //删除成功后，查询最后一页
                            vue.list(999);
                        }
                        else{
                            vue.list(0);
                        }
                    });
                }

            }
        });
    });

</script>

<!-- 页面可变内容 -->
<div class="container-fluid" id="container-fluid">

    <ol class="breadcrumb">
        <li><a href="listCategory">所有分类</a></li>
        <li><a :href="'listProperty?cid=' + property.cid">{{category.name}}</a></li>
        <li class="active">编辑属性</li>
    </ol>

    <!--    表格信息-->
    <div class="row">
        <main role="main" class="col-md-10 ml-sm-auto col-lg-10 pt-3 px-4 col-lg-offset-1">
            <!--  表头-->
            <div class="row">
                <div class="col-xs-6 col-sm-10">
                    <h4>属性管理</h4>
                </div>
            </div>
            <!--  表格内容-->
            <div class="table-responsive">
                <table class="table table-striped table-sm">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th>属性名称</th>
                        <th>编辑</th>
                        <th>删除</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr v-for="list in beans.list">

                        <td>{{list.id}}</td>
                        <td>{{list.name}}</td>
                        <td>
                            <a :href="'editProperty?id=' + list.id "><span class="glyphicon glyphicon-edit"></span></a>
                        </td>
                        <td>
                            <a href="#nowhere"  @click="deleteProperty(list.id)"><span class="   glyphicon glyphicon-trash"></span></a>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>

        </main>
    </div>

    <!--分页-->
    <div class="row col-md-offset-5">
        <nav aria-label="Page navigation">
            <ul class="pagination">

                <li :class="{disabled: beans.isFirstPage}">
                    <a href="#" @click="jump('first')"><span>首页</span></a>
                </li>
                <li :class="{disabled: !beans.hasPreviousPage}">
                    <a href="#" aria-label="Previous" @click="jump('pre')">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                </li>

                <li v-for="navigatepageNum in beans.navigatepageNums"><a href="#" @click="jumpByNumber(navigatepageNum)">{{navigatepageNum}}</a></li>

                <li :class="{disabled: !beans.hasNextPage}">
                    <a href="#" aria-label="Next" @click="jump('next')">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
                <li :class="{disabled: beans.isLastPage}">
                    <a href="#" @click="jump('last')"><span>尾页</span></a>
                </li>

            </ul>
        </nav>
    </div>

    <!-- 新增分类框 -->
    <div class="row">
        <div class="panel panel-warning addDiv">
            <div class="panel-heading">新增属性</div>
            <div class="panel-body">
                <table class="addTable">
                    <tr>
                        <td>属性名称</td>
                        <td><input  @keyup.enter="add" v-model.trim="property.name" type="text" class="form-control"></td>
                    </tr>
                    <tr class="submitTR">
                        <td colspan="2" align="center">
                            <a href="#nowhere"  @click="add" class="btn btn-success">提交</a>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </div>

</div>

</body>

</html>